package com.SmileBreaker;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Vibrator;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.WindowManager;
import android.widget.Toast;

import com.Data.Account;
import com.Data.Configuration;
import com.StageEditor.SelectEditStageNumberActivity;

public class GameStartActivity extends Activity {
	
	// Activity 변수
	private ImageButton btn_newGame;
	private ImageButton btn_continue;
	private ImageButton btn_config;
	private ImageButton btn_stageeditor;
	private ImageButton btn_masterAccount;
	private ImageButton btn_help;
	private Bitmap background;
	private Bitmap background_buttons;
	private Bitmap titleBitmap;
	private int count;
	private GameStartView view;
	
	// 게임 심의 등급 출력 관련 변수
	private Bitmap gameRating;
	
	//sound flag
	private boolean isFirst = true;
	private boolean intendPause = false;
	private boolean backPause = false;
	
	// 개발자모드
	private boolean isDeveloperMode = false;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {// 화면 정보
        super.onCreate(savedInstanceState);

		// 자동 잠금 방지
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }

	@Override
	protected void onStart() {
    	initData();
		view = new GameStartView(this, null);
        setContentView(view);
		
		super.onStart();
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		
		////hs// 배경음악 다시 시
        if(!isFirst){
			SoundManager.bgmRestart();
        }
		isFirst = false;
		intendPause = false;
		backPause = false;
		////hs
	}
	
    private void initData() {
       	getDiaplayInfo();
		
    	// 이미지버튼 생성
    	int BTN_WIDTH = (int)(500 * DisplayInfo.WHOLE_RATIO);
    	int BTN_HEIGHT = (int)(120 * DisplayInfo.WHOLE_RATIO);
    	int x = DisplayInfo.LOGICAL_WIDTH/2 - BTN_WIDTH - (int)(100 * DisplayInfo.WHOLE_RATIO);
    	int y = DisplayInfo.LOGICAL_HEIGHT/2 - BTN_HEIGHT/2 - (int)(40 * DisplayInfo.WHOLE_RATIO);
    	Bitmap img_newGame = BitmapFactory.decodeResource(getResources(), R.drawable.btn_new_start);
    	Bitmap img_continue = BitmapFactory.decodeResource(getResources(), R.drawable.btn_continue);
    	Bitmap img_config = BitmapFactory.decodeResource(getResources(), R.drawable.btn_config);
    	Bitmap img_stageeditor = BitmapFactory.decodeResource(getResources(), R.drawable.btn_stage_editor);
    	Bitmap img_help = BitmapFactory.decodeResource(getResources(), R.drawable.btn_help);
    	btn_newGame = new ImageButton(x, y, BTN_WIDTH, BTN_HEIGHT, img_newGame);
    	y += (int)(140 * DisplayInfo.WHOLE_RATIO);
    	btn_continue = new ImageButton(x, y, BTN_WIDTH, BTN_HEIGHT, img_continue);
    	y += (int)(140 * DisplayInfo.WHOLE_RATIO);
    	btn_config = new ImageButton(x, y, BTN_WIDTH, BTN_HEIGHT, img_config);
    	y += (int)(140 * DisplayInfo.WHOLE_RATIO);
    	btn_help = new ImageButton(x, y, BTN_WIDTH, BTN_HEIGHT, img_help);
    	x += (int)(520 * DisplayInfo.WHOLE_RATIO);
    	btn_stageeditor = new ImageButton(x, y, BTN_WIDTH, BTN_HEIGHT, img_stageeditor);
    	btn_masterAccount = new ImageButton((int)(1150 * DisplayInfo.WHOLE_RATIO), (int)(470 * DisplayInfo.WHOLE_RATIO), BTN_HEIGHT, BTN_HEIGHT, img_stageeditor);
    	
    	// 타이틀
    	titleBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.title_smilebreaker);
    	titleBitmap = Bitmap.createScaledBitmap(titleBitmap, (int)(DisplayInfo.LOGICAL_WIDTH/1.5), DisplayInfo.LOGICAL_HEIGHT/4, false);
    	
    	// 배경화면 생성
    	background = BitmapFactory.decodeResource(getResources(), R.drawable.background);
    	background = Bitmap.createScaledBitmap(background, DisplayInfo.LOGICAL_WIDTH, DisplayInfo.LOGICAL_HEIGHT, false);
    	background_buttons = BitmapFactory.decodeResource(getResources(), R.drawable.background_buttons);
    	background_buttons = Bitmap.createScaledBitmap(background_buttons, DisplayInfo.LOGICAL_WIDTH*3/5, DisplayInfo.LOGICAL_HEIGHT, false);
    	
    	// 게임 등급 관련 이미지 초기화
    	gameRating = BitmapFactory.decodeResource(getResources(), R.drawable.game_rating);
    	gameRating = Bitmap.createScaledBitmap(gameRating, (int)(280*DisplayInfo.WHOLE_RATIO), (int)(340*DisplayInfo.WHOLE_RATIO), false);
    }
    
    @Override
	protected void onPause() {
		super.onPause();
		
		////hs
		if(!(intendPause || backPause)){
			 SoundManager.bgmPause();
		}
		////hs
	}
    
	@Override
	protected void onDestroy() {
		btn_newGame.getBtnImage().recycle();
		btn_continue.getBtnImage().recycle();
		btn_config.getBtnImage().recycle();
		btn_masterAccount.getBtnImage().recycle();
		background.recycle();
		background_buttons.recycle();
		titleBitmap.recycle();
		
		super.onDestroy();
	}

	/**
	 * 화면 출력 장치의 정보를 가져온다.
	 */
	public void getDiaplayInfo() {
		// conf를 불러온다.
		Configuration conf = new Configuration();
		conf.load();
		SoundManager.setVolume(conf.getVol());
		DisplayInfo.setVideo(conf.getVideo());
		
		// 장치 해상도 구하기
		DisplayMetrics displayMetrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
		int displayWidth = displayMetrics.widthPixels;
		int displayHeight = displayMetrics.heightPixels;
		
		if(displayWidth < displayHeight) {
			int tmp = displayWidth;
			displayWidth = displayHeight;
			displayHeight = tmp;
		}
		// 장치 해상도 관련 정보 생성
		DisplayInfo.setData(displayWidth, displayHeight);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		super.onTouchEvent(event);
		
		if(event.getAction() == MotionEvent.ACTION_DOWN) {
			if(btn_newGame.IsClicked(event.getX(),event.getY())){
				Intent intent = new Intent(GameStartActivity.this, NewGameActivity.class);
				startActivity(intent);
				////hs
				intendPause = true;
				isFirst = true;
				////hs
				
				return true;
			} else if(btn_continue.IsClicked(event.getX(),event.getY())){
				Intent intent = new Intent(GameStartActivity.this, ContinueActivity.class);
				startActivity(intent);
				////hs
				intendPause = true;
				isFirst = true;
				////hs
				
				return true;
			} else if(btn_config.IsClicked(event.getX(),event.getY())){
				Intent intent = new Intent(GameStartActivity.this, ConfigurationActivity.class);
				startActivity(intent);
				////hs
				intendPause = true;
				isFirst = true;
				////hs
				
				return true;
			} else if(btn_help.IsClicked(event.getX(),event.getY())){
				Intent intent = new Intent(GameStartActivity.this, HelpActivity.class);
				startActivity(intent);
				////hs
				intendPause = true;
				isFirst = true;
				////hs
				
				return true;
			} else if(isDeveloperMode && btn_stageeditor.IsClicked(event.getX(),event.getY(),1)){
				Intent intent = new Intent(GameStartActivity.this, SelectEditStageNumberActivity.class);
				startActivity(intent);
				////hs
				intendPause = true;
				isFirst = true;
				////hs
				
				return true;
			} else if(btn_masterAccount.IsClicked(event.getX(),event.getY(),ImageButton.MUTE)){
				count++;
				// 마스터 계정 생성
				if(count == 20) {
					// 알림 발생
					SoundManager.play(SoundManager.MENU);
					Vibrator vibe = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
					vibe.vibrate(80);
					//계정 생성 및 저장
					Account account = new Account(this);
					account.setClearStage(40);
					for (int i = 0; i < account.getChars().size(); i++) {
						account.getChars().get(i).setDoHave(true);
					}
					account.save(3);
					Toast toast = Toast.makeText(this, "Master account is created!!! SaveSlot is #3.", Toast.LENGTH_SHORT);
					toast.show();
				}
				return true;
			}
		}
		
		return false;
	}
	
	@Override
	public void onBackPressed() {
		super.onBackPressed();
		backPause = true;
		isFirst = true;
		finish();
	}
	
	public Bitmap getBackground() {	return background;	}
	public Bitmap getBackground_buttons() {	return background_buttons;	}
	public Bitmap getTitleBitmap() {	return titleBitmap;	}
	public ImageButton getBtn_newGame() {	return btn_newGame;	}
	public ImageButton getBtn_continue() {	return btn_continue;	}
	public ImageButton getBtn_config() {	return btn_config;	}
	public ImageButton getBtn_help() {	return btn_help;	}
	public ImageButton getBtn_stageeditor() {	return btn_stageeditor;	}
	public ImageButton getBtn_masterAccount() {	return btn_masterAccount;	}
	public boolean isDeveloperMode() {	return isDeveloperMode;	}
	public Bitmap getGameRating() {	return gameRating;	}
	
}